package com.qf.service.impl;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.utils.UserDataStore;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public List<User> addUser(User user) {
        Integer integer = UserDataStore.idgenerator++;
        user.setId(integer);
        UserDataStore.userList.add(user);
        return UserDataStore.userList;
    }

    @Override
    public void edit(User user) {
        int index = -1;
        Integer id = user.getId();
        for (int i = 0; i < UserDataStore.userList.size(); i++) {
            if (id.equals(UserDataStore.userList.get(i).getId())) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            UserDataStore.userList.set(index, user);
        }
    }

    @Override
    public List<User> list() {
        return UserDataStore.userList;
    }

    @Override
    public void delete(Integer id) {
        if(id>=0 && id<UserDataStore.userList.size()){
            UserDataStore.userList.remove(id);
        }
    }

    @Override
    public User findbyid(Integer id) {
        if(id>=0 && id<UserDataStore.userList.size()) {
            return UserDataStore.userList.get(id);
        }
        return null;
    }
}
